<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<input id="search" type="search" onsearch="searchEventDispatched()" />
<p id="description">

</p>

<script>
var searchEventObserved;

description("This tests if the value in a search input form is cleared and a 'search' event is triggered, when we press the Escape key. " +
            "To run (a part of) this test manually, type some text in the search form and then press the Escape key. " +
            "If the text is cleared, then the test passes.");

if (window.testRunner && window.eventSender) {
    var input = $("search");
    var enabled = false;
    var disabled = true;
    var readonly = true;

    searchEventObserved = false;
    input.focus();
    setInputAttributes(input, "foo", enabled);
    eventSender.keyDown("Escape");
    shouldBeEqualToString('input.value', "");
    shouldBe('searchEventObserved', 'true');
    input.blur();

    searchEventObserved = false;
    input.focus();
    setInputAttributes(input, "", enabled);
    eventSender.keyDown("Escape");
    shouldBeEqualToString('input.value', "");
    shouldBe('searchEventObserved', 'true');
    input.blur();

    searchEventObserved = false;
    input.focus();
    setInputAttributes(input, "foo", enabled, readonly);
    eventSender.keyDown("Escape");
    shouldBeEqualToString('input.value', "foo");
    shouldBe('searchEventObserved', 'false');
    input.blur();

    searchEventObserved = false;
    input.focus();
    setInputAttributes(input, "foo", disabled);
    eventSender.keyDown("Escape");
    shouldBeEqualToString('input.value', "foo");
    shouldBe('searchEventObserved', 'false');
    input.blur();

    searchEventObserved = false;
    input.focus();
    setInputAttributes(input, "foo", disabled, readonly);
    eventSender.keyDown("Escape");
    shouldBeEqualToString('input.value', "foo");
    shouldBe('searchEventObserved', 'false');
    input.blur();
}

function setInputAttributes(input, text, disabled, readonly) {
    input.value = text;
    input.disabled = disabled;
    input.readOnly = !!readonly;
}

function searchEventDispatched() {
    searchEventObserved = true;
}
</script>
</body>
</html>
